package com.blog.blogvue.controller;

import com.blog.admin.mapper.BlogMessageMapper;
import com.blog.admin.mapper.apiMapper.AboutMapper;
import com.blog.admin.service.BlogMessageService;
import com.blog.admin.vo.BlogCommentVO;
import com.blog.admin.vo.BlogMessageVO;
import com.blog.admin.vo.BlogResourcesVO;
import com.blog.common.ApiIntercept;
import com.blog.util.DateUtil;
import com.blog.util.DateUtils;
import com.blog.util.Result;
import com.blog.vo.ConfigVO;
import com.blog.vo.PageVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author tangqj
 * @Description 关于本站模块和其他模块
 * @create 2020/3/12 14:32
 */
@Controller
@RequestMapping("/api/AboutApiController")
public class AboutApiController {
    @Autowired
    AboutMapper aboutMapper;
    @Autowired
    BlogMessageService blogMessageService;
    @Autowired
    BlogMessageMapper blogMessageMapper;

    /**
     * 获取友情链接
     *
     * @param response
     */
    @GetMapping("getBlogLink")
    public void getBlogLink(HttpServletResponse response) {
        Result.print(response, Result.responeAjaxMap(true, "", aboutMapper.getBlogLink(), null));
    }


    /**
     * 分页获取留言
     *
     * @param response
     * @param page
     */
    @GetMapping("getMessage")
    public void getMessage(PageVO page, HttpServletResponse response) {
        PageInfo<BlogMessageVO> pageInfo = blogMessageService.getBlogMessage(page, null);
        for (BlogMessageVO pageBlogMessageVO : pageInfo.getList()) {
            BlogMessageVO blogMessageVO = new BlogMessageVO();
            blogMessageVO.setId(pageBlogMessageVO.getId());
            List<BlogMessageVO> reply_blogCommentVOS = blogMessageMapper.getBlogMessage(blogMessageVO);
            pageBlogMessageVO.setReply(reply_blogCommentVOS);
        }
        Result.print(response, Result.responeAjaxMap(true, "", pageInfo, null));
    }

    /**
     * 评论插入
     *
     * @param response
     * @param blogMessageVO
     */
    @PostMapping("insertBlogMessage")
    @ApiIntercept
    public void insertBlogMessage(@RequestBody Map<String, Object> param, HttpServletResponse response, BlogMessageVO blogMessageVO) throws Exception {
        blogMessageVO = Result.mapToBean(param, BlogMessageVO.class);
        // 对应数据转换
        int result = blogMessageMapper.insertBlogMessage(blogMessageVO);
        if (result > 0) {
            Result.print(response, Result.responeAjaxMap(true, "评论成功", blogMessageVO, null));

        } else {
            Result.print(response, Result.responeAjaxMap(false, "", null, null));
        }
    }

    /**
     * 获取点点滴滴
     *
     * @param response
     */
    @GetMapping("getBlogResourcesDD")
    public void getBlogResourcesDD(HttpServletResponse response) {
        List<Map<String, Object>> data = aboutMapper.getBlogResourcesDD();

        Result.print(response, Result.responeAjaxMap(true, "", getTime(data), null));
    }

    /**
     * 获取资源下载标签
     *
     * @param response
     */
    @GetMapping("getBlogResourcesTitle")
    public void getBlogResourcesTitle(HttpServletResponse response) {
        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "源码");
        map.put("value", "0");
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "工具");
        map1.put("value", "1");
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "电子书");
        map2.put("value", "2");
        data.add(map);
        data.add(map1);
        data.add(map2);
        Result.print(response, Result.responeAjaxMap(true, "", data, null));
    }

    /**
     * 获取资源下载
     *
     * @param response
     */
    @GetMapping("getBlogResources")
    public void getBlogResources(BlogResourcesVO blogResourcesVO, HttpServletResponse response) {
        List<Map<String, Object>> data = aboutMapper.getBlogResources(blogResourcesVO.getId(),blogResourcesVO.getResourceType(),blogResourcesVO.getTitle());
        Map<String,Object> map = new HashMap<>();
        map.put("ip","http://" + ConfigVO.ip + ":" + ConfigVO.port + "/");
        Result.print(response, Result.responeAjaxMap(true, "", data, map));
    }

    /**
     * 将数据库的时间进行格式化
     *
     * @param data
     */
    public List<LinkedHashMap<String, Object>> getTime(List<Map<String, Object>> data) {
        //封装的结果集
        List<LinkedHashMap<String, Object>> years = new ArrayList<>();
        //获取12个月份
        List<String> mouths = new ArrayList<>();
        for (int i = 12; i > 0; i--) {
            String mouth = String.format("%2d", i).replace(" ", "0");
            mouths.add("" + mouth + "");
        }
        //遍历数据，将时间拆分为年月日分别储存
        for (Map<String, Object> map : data) {
            Date date = (Date) map.get("create_time");
            String year = DateUtils.getStringTime(date, DateUtils.yyyy);
            String mouth = DateUtils.getStringTime(date, DateUtils.MM);
            String time = DateUtils.getStringTime(date, DateUtils.MM_dd_hms);
            map.put("year", year);
            map.put("mouth", mouth);
            map.put("create_time", time);
        }
        boolean isNull;
        for (Map<String, Object> map : data) {
            isNull = true;
            //遍历数据集合
            for (LinkedHashMap<String, Object> datas : years) {
                // 如果找到与当前年份相符合的数据
                if (map.get("year").equals(datas.get("year"))) {
                    LinkedHashMap<String, Object> mouthDatas = (LinkedHashMap<String, Object>) datas.get("mouth");
                    // 判断是否有当前月份的数据
                    if (mouthDatas.containsKey(map.get("mouth"))) {
                        List<Map<String, Object>> mouthData = (List<Map<String, Object>>) mouthDatas.get(map.get("mouth"));
                        mouthData.add(map);
                    } else {
                        // 没有的话新建
                        List<Map<String, Object>> mouthData = new ArrayList<>();
                        mouthData.add(map);
                        mouthDatas.put(map.get("mouth").toString(), mouthData);
                    }
                    isNull = false;
                    break;
                }
            }
            // 当前年份数据集合中还不存在的时候
            if (isNull) {
                LinkedHashMap<String, Object> newDatas = new LinkedHashMap<>();
                LinkedHashMap<String, List<Map<String, Object>>> mouthDatas = new LinkedHashMap<>();
                List<Map<String, Object>> mouthList = new ArrayList<>();
                mouthList.add(map);
                mouthDatas.put(map.get("mouth").toString(), mouthList);
                newDatas.put("year", map.get("year"));
                newDatas.put("mouth", mouthDatas);
                years.add(newDatas);
            }

        }
        return years;
    }
}
